package com.truyensex.ui;

import info.androidhive.doc18.R;

import java.util.ArrayList;

import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.truyensex.adapter.ListArticleAdapter;
import com.truyensex.dao.truyenManager;
import com.truyensex.model.Article;

public class ListArticleFragment extends Fragment{
	//--Khai bao bien
	private TypedArray navMenuIcons;
	private ListView lvListArticle;
	private ListArticleAdapter adapterListArticleAdapter;
	private TextView textview;
	private ArrayList<Article> dsTruyen = new ArrayList<Article>();
	
	
	
	//--Ket thuc khai bao bien
	
	
	
	//--Khoi tao cac ham 
	
	//--Ket thuc khoi tao cac ham
	public ListArticleFragment()
	{
		dsTruyen = truyenManager.listArticle;
	}
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.fragment_listarticle, container, false);
        navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
        lvListArticle = (ListView) rootView.findViewById(R.id.lvListArticle);
        textview = (TextView) rootView.findViewById(R.id.textView1);
        //instanceData();
        //dsTruyen = readXmlFile("test.xml");
		adapterListArticleAdapter = new ListArticleAdapter(getActivity(),
				dsTruyen);
		lvListArticle.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				adapterListArticleAdapter.notifyDataSetChanged();
			}
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
		adapterListArticleAdapter.notifyDataSetChanged();
		lvListArticle.setAdapter(adapterListArticleAdapter);
		lvListArticle.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Fragment fragment = new ArticleFragment(dsTruyen.get(arg2));
				if (fragment != null) {
					FragmentManager fragmentManager = getFragmentManager();
					fragmentManager.beginTransaction()
							.replace(R.id.frame_container, fragment).addToBackStack(null).commit();
				} else {
					Log.e("MainActivity", "Error in creating fragment");
				}
			}
			
		});
        return rootView;
    }
	private void getData() 
	{
		/*ArrayList<Article> listArticle = Helper.readXml("https://www.dropbox.com/s/d4iz99jglwbincf/Test.xml");
		textview.setText(listArticle.get(0).getContent());*/
	}
	/*public ArrayList<Article> readXmlFile(String path)
	{
		ArrayList<Article> listArticle = new ArrayList<Article>();
		try
		{
			InputStream is = getActivity().getAssets().open(path);
			listArticle = Helper.readXmlFromInputStream(is);
			Log.e("aa", "size: " + listArticle.size());
			for(int i=0;i<listArticle.size();i++)
			{
				Log.e("aa", "msg: " + listArticle.get(i).getName());
			}
			
		}catch(IOException ex)
		{
			Log.e("exp", ex.getMessage());
		}
		return listArticle;
	}*/
	/*private void instanceData()
	{
		for(int i=0;i<10;i++)
		{
			NavDrawerItem item = new NavDrawerItem("article: " + i,navMenuIcons.getResourceId(4, -1), true, "22");
			listArticle.add(item);
		}
	}*/

}
